package com.itheima.HomeWork_day14;

import java.util.concurrent.locks.ReentrantLock;

public class LockTest02 {
    public static void main(String[] args) {
        Ticket t=new Ticket();
        Thread t1=new Thread(t);
        Thread t2=new Thread(t);
        Thread t3=new Thread(t);

        t1.setName("窗口A");
        t2.setName("窗口B");
        t3.setName("窗口C");

        t1.start();
        t2.start();
        t3.start();


    }
}
class Ticket implements Runnable{

    //票的数量
    private int ticket=90;
    private  Object obj=new Object();
    private ReentrantLock lock=new ReentrantLock();

    @Override
    public void run() {

        while(true) {
        //    synchronized (obj) {
                try {
                    //使用lock
                    lock.lock();
                    if (ticket <= 0) {
                        System.out.println("票已经售完！");
                        break;
                    } else {
                        Thread.sleep(100);
                        ticket--;
                        System.out.println(Thread.currentThread().getName() +
                                "正在售票ing..还剩" + ticket + "张");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally{
                    lock.unlock();
            }


        }
        }
    }